<?php
namespace application\system\model\auth;
/**
 * Description of Model
 *
 * Model 	: user
 * Created	: 02Oct2014
 * Purpose 	: User Model
 *
 * Change Logs
 * -----------------------------------------------------------
 * 02Oct2014 admin: Created the model user
 *  
 * @author admin
 *
 * Properties
 * @property bigint $user_id User id
 * @property Boolean $user_active User active
 * @property String $user_name User name
 * @property String $user_password User password
 */
class User extends \simbola\core\application\AppModel{
    static  //config params
            $table_name, 
            $primary_key, 
            $class_name, 
            //state machine
            $state_config,
            //relationships
            $has_many = array(), 
            $belongs_to = array(), 
            //alias
            $alias_attribute = array(),
            //validations system
            $validates_uneditable = array(),
            //validations php-ar 
            $validates_presence_of = array(),
            $validates_size_of = array(),
            $validates_length_of = array(),
            $validates_exclusion_of = array(),
            $validates_inclusion_of = array(),
            $validates_format_of = array(),
            $validates_numericality_of = array(),
            $validates_uniqueness_of = array();

    public static function initialize() {
        //Model Setup
        self::setClass(__CLASS__);
        self::setSource('system', 'auth', 'user');
        self::primaryKey(array('user_id'));        

        self::stateMachine(array(
            'states' => array('active', 'deactive'),
            'rules' => array(
                array('start' => 'active'),
                array('from' => 'active', 'to' => 'deactive'),
                array('from' => 'deactive', 'to' => 'active'),
                array('end' => 'deactive')
            )
        ));
        
        //Relationships - Has Many        
        self::hasMany(array('roles', 'class_name' => '\application\system\model\auth\RoleAssign', 'foreign_key' => 'user_id', 'primary_key' => 'user_id'));
        self::hasMany(array('sessions', 'class_name' => '\application\system\model\auth\Session', 'foreign_key' => 'user_id', 'primary_key' => 'user_id'));
        self::hasMany(array('jobs', 'class_name' => '\application\system\model\transaction\Job', 'foreign_key' => 'user_id', 'primary_key' => 'user_id'));

        //Relationships - Belongs To
        //None

        //Validations
        // - user_id        
        // - user_active
        // - user_name
        self::validateUniquenessOf(array("user_name"));
        self::validateSizeOf(array("user_name", "maximum" => 100));
        // - user_password
        self::aliasAttribute('user_name', 'username');               
        
        self::validateUneditable('user_id');
    }
    
    public function changePassword($current, $new, $repeat) {
        $rbap = \simbola\Simbola::app()->auth->getRBAP();
        $data['status'] = false;
        if (!$rbap->user_authenticate($this->user_name, $current)) {
            $data['message'] = AuthUser::Term("change_password.authentication_failed");
        } elseif ($new != $repeat) {
            $data['message'] = AuthUser::Term("change_password.password_mismatch");
        } else {
            $rbap->user_reset_password($this->user_name, $new);
            $data['status'] = true;
            $data['message'] = AuthUser::Term("change_password.success");
        }
        return $data;
    }
    
    public function save($validate = true) {
        if($this->attribute_is_dirty('user_password')){
            $this->user_password = md5($this->user_password);
        }
        return parent::save($validate);
    }
}
?>
